package com.ymatou.iapi.livemanage.testcase;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livemanage.service.DelayActivityCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtLive;
import com.ymttest.database.model.YmtProductsInLive;
import com.ymttest.database.mongo.LiveBasicInfoWapper;
import com.ymttest.database.mongo.LiveModelWapper;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.database.mongo.jproduct.LivesWapper;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/**
*/
@TestSuite
public class Ts_DelayActivity {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播Admin-延长直播时间");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new DelayActivityCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DelayActivity_001() {
		Logger.start(true, "直播Admin-延长直播时间");
		try {

			// 1 创建直播
			JSONObject createLiveReq = LiveCallService.createLiveBeginInFuture(sellerName, sellerId);
			int liveId = createLiveReq.getJSONObject("Data").getInt("ActivityId");

			// 2 获取结束时间
			YmtLiveWapper ymtLiveWapper = new YmtLiveWapper();
			YmtLive ymtLive = ymtLiveWapper.selectByLiveId(liveId);

			// 2 延期直播
			Map<String, Object> updateMap = new HashMap<String, Object>();

			Calendar c = Calendar.getInstance();
			c.setTime(ymtLive.getDendtime());

			c.add(Calendar.DAY_OF_MONTH, 1);
			updateMap.put("DelayTime", YMTDateUtil.getYMDHMSFormat().format(c.getTime()));

			JSONObject delayLiveRet = LiveCallService.delayLive(sellerId, liveId, updateMap);
			Logger.verifyEquals(200, delayLiveRet.getInt("Code"), "响应报文-返回码");

			// SQL数据库
			ymtLive = ymtLiveWapper.selectByLiveId(liveId);
			Logger.verifyEquals(c.getTime(), ymtLive.getDendtime(), "数据库SQL-Activity 直播结束时间");

			// Mongo数据库
			if (YmtMongoDriverFactroy.isOldMongoCheck()) {
				String mongoEndDate = YMTDateUtil
						.getFromISODate(new LiveBasicInfoWapper().getByActivityId(liveId).getEndTime().toString());
				String mongoEndDateBj = YMTDateUtil.getBeforeOrNextHour(mongoEndDate, 8);
				// Logger.verifyEquals(c.getTime(),
				// YMTDateUtil.parseYMDHMSDate(mongoEndDateBj),"数据库oldMongo-Activity
				// 直播结束时间");
				AssertService.assertTime(c.getTime(), YMTDateUtil.parseYMDHMSDate(mongoEndDateBj), 30);
			}

			if (YmtMongoDriverFactroy.isNewMongoCheck()) {
				LiveModelWapper liveModelWapper = new LiveModelWapper();
				Map<String, Object> liveModel = liveModelWapper.getActivityId(liveId);
				String mongoEndDate = YMTDateUtil.getObjectFormatTime(liveModel.get("EndTime"), "yyyy-MM-dd HH:mm:ss");
				// Logger.verifyEquals(c.getTime(),
				// YMTDateUtil.parseYMDHMSDate(mongoEndDate),"数据库newMongo-Activity
				// 直播结束时间");
				AssertService.assertTime(c.getTime(), YMTDateUtil.parseYMDHMSDate(mongoEndDate), 30);
			}

			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()) {
				LivesWapper liveModelWapper = new LivesWapper();
				Map<String, Object> liveModel = liveModelWapper.getByActivityId(liveId);
				String mongoEndDate = YMTDateUtil.getObjectFormatTime(liveModel.get("end"), "yyyy-MM-dd HH:mm:ss");
				AssertService.assertTime(c.getTime(), YMTDateUtil.parseYMDHMSDate(mongoEndDate), 30);

				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId);
			}

		} catch (Exception e) {
			Logger.fail(e);
		}

	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DelayActivity_002() {
		Logger.start(true, "直播Admin-延长直播时间");
		try {

			// 1 创建直播
			JSONObject createLiveReq = LiveCallService.createLiveBeginInFuture(sellerName, sellerId);
			int liveId = createLiveReq.getJSONObject("Data").getInt("ActivityId");

			// 2 获取结束时间
			YmtLiveWapper ymtLiveWapper = new YmtLiveWapper();
			YmtLive ymtLive = ymtLiveWapper.selectByLiveId(liveId);

			// 3 add new product
			JSONObject productBean = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null);
			String productId = productBean.getJSONObject("Data").getString("ProductID");

			// 4 add product to live
			JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			Logger.verifyEquals(200, addProductInLiveBean.getInt("Code"), "响应报文-返回码");

			// 5 延期直播
			Map<String, Object> updateMap = new HashMap<String, Object>();

			Calendar c = Calendar.getInstance();
			c.setTime(ymtLive.getDendtime());

			c.add(Calendar.DAY_OF_MONTH, 1);
			updateMap.put("DelayTime", YMTDateUtil.getYMDHMSFormat().format(c.getTime()));

			JSONObject delayLiveRet = LiveCallService.delayLive(sellerId, liveId, updateMap);
			Logger.verifyEquals(200, delayLiveRet.getInt("Code"), "响应报文-返回码");

			// SQL数据库
			ymtLive = ymtLiveWapper.selectByLiveId(liveId);
			Logger.verifyEquals(c.getTime(), ymtLive.getDendtime(), "数据库SQL-Activity 直播结束时间");
			// 直播商品表
			YmtProductsInLive ymtProductsInLive = new YmtProductsInLiveWapper().selectByLiveId(liveId).get(0);
			AssertService.assertTime(c.getTime(), ymtProductsInLive.getDendtime(), 30);

			// Mongo数据库
			if (YmtMongoDriverFactroy.isOldMongoCheck()) {
				String mongoEndDate = YMTDateUtil
						.getFromISODate(new LiveBasicInfoWapper().getByActivityId(liveId).getEndTime().toString());
				String mongoEndDateBj = YMTDateUtil.getBeforeOrNextHour(mongoEndDate, 8);
				AssertService.assertTime(c.getTime(), YMTDateUtil.parseYMDHMSDate(mongoEndDateBj), 30);
			}

			if (YmtMongoDriverFactroy.isNewMongoCheck()) {
				// Mongo LiveModel 直播表
				LiveModelWapper liveModelWapper = new LiveModelWapper();
				Map<String, Object> liveModel = liveModelWapper.getActivityId(liveId);
				String mongoEndDate = YMTDateUtil.getObjectFormatTime(liveModel.get("EndTime"), "yyyy-MM-dd HH:mm:ss");
				AssertService.assertTime(c.getTime(), YMTDateUtil.parseYMDHMSDate(mongoEndDate), 30);

				// Mongo LiveProductModel 直播商品表
				Map<String, Object> liveProductModelMap = new LiveProductModelWapper().getActivityId(liveId);
				String liveProductModelEndDate = YMTDateUtil.getObjectFormatTime(liveProductModelMap.get("EndTime"),
						"yyyy-MM-dd HH:mm:ss");
				AssertService.assertTime(c.getTime(), YMTDateUtil.parseYMDHMSDate(liveProductModelEndDate), 30);

			}

			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()) {
				// Mongo LiveModel 直播表
				LivesWapper liveModelWapper = new LivesWapper();
				Map<String, Object> liveModel = liveModelWapper.getByActivityId(liveId);
				String mongoEndDate = YMTDateUtil.getObjectFormatTime(liveModel.get("end"), "yyyy-MM-dd HH:mm:ss");
				AssertService.assertTime(c.getTime(), YMTDateUtil.parseYMDHMSDate(mongoEndDate), 30);

				// Mongo LiveProductModel 直播商品表
				Map<String, Object> liveProductModelMap = new LiveProductsWapper().getByActivityId(liveId);
				String liveProductModelEndDate = YMTDateUtil.getObjectFormatTime(liveProductModelMap.get("end"),
						"yyyy-MM-dd HH:mm:ss");
				AssertService.assertTime(c.getTime(), YMTDateUtil.parseYMDHMSDate(liveProductModelEndDate), 30);

				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}

	}

}